package pl.un4giv3n.mightvsmagic.admin.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.action.PersistenceAction;
import pl.un4giv3n.mightvsmagic.entity.User;

/**
 * @author andrzejewskia
 * delete player
 */
public class BanPlayerPageAction extends PersistenceAction {

	private static final long serialVersionUID = 1L;
	private static final DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
	private long playerId;
	private User player;
	private String blockedReason;
	private String blockedUntil;
	/**
	 * session user to be deleted
	 */
	private User admin;

	/* (non-Javadoc)
	 * @see pl.un4giv3n.mightvsmagic.action.PersistenceAction#execute()
	 */
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		admin = getSessionUser(entityManager);
		if (admin == null) {
			addActionError("Zaloguj się");
			entityManager.close();	
			return LOGIN;
		}
		player = entityManager.find(User.class, playerId);
		if(player == null){
			addActionError("Nieprawidłowe id usera");
			entityManager.close();	
			return ERROR;
		}
		if(player.isBlocked()){
			blockedUntil = player.getBlockedUntil() == null ? "DOŻYWOTNIO" : sdf.format(player.getBlockedUntil());
			blockedReason = player.getBlockReason();
		}
		entityManager.close();
		return SUCCESS;
	}

	public long getPlayerId() {
		return playerId;
	}

	public void setPlayerId(long playerId) {
		this.playerId = playerId;
	}

	public User getPlayer() {
		return player;
	}

	public String getBanned(){
		return player.isBlocked() ? "ZABLOKOWANY" : "AKTYWNY";
	}

	public String getBlockedReason() {
		return blockedReason;
	}

	public void setBlockedReason(String blockedReason) {
		this.blockedReason = blockedReason;
	}

	public String getBlockedUntil() {
		return blockedUntil;
	}

	public void setBlockedUntil(String blockedUntil) {
		this.blockedUntil = blockedUntil;
	}
	
}
